/* options.c - options handling
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "options.h"
#include "utils.h"

void print_help(void) {
	printf("\nUsage:\n");
	printf("nmanga [options] LINKS...\n");

	printf("\nOptions:\n");
	printf("-h, --help                  - print this message\n");
	printf("-l, --links-only            - print image links and exit\n");
	printf("-r, --reader=READER_NAME    - set reader name manually\n");
	printf("                              available options:\n");
	printf("                                                 readmanga,\n");
	printf("                                                 mintmanga\n");
	printf("-s, --stdin                 - read chapter page from stdin\n");
	printf("                              WARNING: -r option must be set\n");
	printf("-v, --verbose               - be verbose\n");

	exit(EXIT_SUCCESS);
}

struct options *
scan_options(int argc, char **argv, struct options *opts) {
	/* iteration variables declaration */
	int i;
	int ii;
	int c;

	/* normal variables declaratio */
	char *urn;    /* reader name entered by user */

	/* setting default options */
	opts->links_only      = 0;
	opts->read_from_stdin = 0;
	opts->reader_name     = UNDEFINED;

	if (argc > 1) {
		/* looking for user input */
		for (i = 1; i < argc; i++) {
			if (argv[i][0] == '-' ||
					strstr(argv[i], "--") != NULL) {

				/* '-h' and '--help' options */
				if (strcmp("-h", argv[i]) == 0 ||
						strcmp("--help", argv[i]) == 0) {

					print_help();

				/* '-l' and '--links-only' options */
				} else if (strcmp("-l", argv[i]) == 0 ||
									 strcmp("-links-only", argv[i]) == 0) {

					opts->links_only=1;

				/* '-r' and '--reader' options */
				} else if (strcmp("-r", argv[i]) == 0 ||
									 strstr(argv[i], "--reader=") != 0) {
					if (strcmp("-r", argv[i]) == 0) {
						/* switching to next argument */
						i++;

						/* memory allocation for urn */
						urn = (char *) emalloc(strlen(argv[i]) + 1 *
																	 sizeof(char));

						/* urn generation */
						strcpy(urn, argv[i]);
					} else {
						for(ii = 0; ii < (strlen(argv[i]) + 1); ii++) {	
							if (argv[i][ii] == '=') {
								ii++;
								c = 0;
								while(ii < (strlen(argv[i]) + 1)) {
									/* memory allocation for urn */
									if(c == 0) {
										urn = (char *) emalloc(2 * sizeof(char));
									} else {
										urn = (char *) erealloc(urn, c + 1 *
																						sizeof(char));
										urn[c] = argv[i][ii];
									}

									/* urn generation */
									urn[c] = argv[i][ii];

									c++;
								}
							}
						}
						urn[c] = '\0';
					}

					/* compare urn and reader names */
					if (strcmp("readmanga", urn) == 0) {
						opts->reader_name = READMANGA;
					} else if (strcmp("mintmanga", urn) == 0) {
						opts->reader_name = MINTMANGA;
					} else {
						printf("FATAL ERROR: Can't determine reader name: %s\n", urn);
						printf("Entered reader name is: %s", urn);
						exit(EXIT_FAILURE);
					}

					free(urn);

				/* '-s' and '--stdin' options */
				} else if (strcmp("-s", argv[i]) == 0 ||
									 strcmp("--stdin", argv[i]) == 0) {
					opts->read_from_stdin = 1;

				/* '-v' and '--verbose' options */
				} else if (strcmp("-v", argv[i]) == 0 ||
									 strcmp("--verbose", argv[i]) == 0) {
					opts->be_verbose = 1;
				} else {
					printf("Can't determine option: %s\n", argv[i]);
					exit(EXIT_FAILURE);
				}
			}
		}
	}

	return opts;
}
